package com.sduept.nwld.dataserver.manager.countanalyse;

import java.time.LocalDate;
import java.util.List;


import javax.inject.Inject;

import com.sduept.cim.ledger.total.model.PrimaryDeviceCount;
import com.sduept.cim.ledger.total.model.ProtectionDeviceCount;
import com.sduept.nwld.dataserver.manager.cim.PrimaryDeviceCountPersistenceManager;
import com.sduept.nwld.dataserver.manager.cim.PrimaryDeviceCountQueryManager;
import com.sduept.nwld.dataserver.manager.cim.ProtectionDeviceCountPersistManager;
import com.sduept.nwld.dataserver.manager.cim.ProtectionDeviceCountQueryManager;
import com.sduept.nwld.dataserver.manager.statistics.ElectricityGenerationManager;
import com.sduept.nwld.dataserver.manager.statistics.ProtectKpiCountManager;
import com.sduept.nwld.dataserver.manager.statistics.ProtectKpiManager;
import com.sduept.nwld.dataserver.manager.statistics.StatisticsCommonManager;
import com.sduept.nwld.dataserver.manager.statistics.TransmissionEquipmentManager;
import com.sduept.nwld.dataserver.model.countanalyse.BusbarInfo;
import com.sduept.nwld.dataserver.model.countanalyse.ElectricityInfo;
import com.sduept.nwld.dataserver.model.countanalyse.FaultFastRemove;
import com.sduept.nwld.dataserver.model.countanalyse.ProtectCorrectAct;
import com.sduept.nwld.dataserver.model.countanalyse.TansformerInfo;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 统计分析类定时任务器
 */
@Component
public class StatisticalAnalysisSchedule {
    @Inject
    public PrimaryDeviceCountQueryManager pdqueryman;
    @Inject
    public PrimaryDeviceCountPersistenceManager pdpisman;

    @Inject
    public ProtectionDeviceCountQueryManager protectQueryManager;
    @Inject
    public ProtectionDeviceCountPersistManager protectPersistManager;

    @Inject
    public ElectricityGenerationManager electricityGenerationManager;

    @Inject
    public TransmissionEquipmentManager transmissionEquipmentManager;

    @Inject
    public ProtectKpiCountManager protectKpiCountManager;

    @Inject
    public ProtectKpiManager protectKpiManager;


    @Inject
    public StatisticsCommonManager statisticsCommonManager;



    /**
     *	 每天执行的任务，每天凌晨23点执行
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void dayExecute() {
        executePrimaryDeviceCount();
        executeProtectDeviceCount();
        executeElectricityInfoCount();
        executeTransmissionInfoCount();
        executeBusbarInfoCount();
        executeFaultFastRemoveCount();
        executeProtectCorrectActCount();
    }

    /**
     * 	一次设备定时统计，每天检测当年的数据是否已经统计，如果没有实时统计当年的
     */
    public void executePrimaryDeviceCount() {
        List<PrimaryDeviceCount> dispatchList = pdqueryman.findDispatchPrimaryDeviceCount(LocalDate.now().getYear());
        if(dispatchList.isEmpty()) {
            pdpisman.persistenceDispatchStatisticCount(false);
        }
        List<PrimaryDeviceCount> maintenanceList = pdqueryman.findMaintenancePrimaryDeviceCount(LocalDate.now().getYear());
        if(maintenanceList.isEmpty()) {
            pdpisman.persistenceMaintenanceStatisticCount(false);
        }
    }
    /**
     * 	保护设备定时统计，每天检测当年的数据是否已经统计，如果没有实时统计当年的
     */
    public void executeProtectDeviceCount() {
        List<ProtectionDeviceCount> dispatchList =  protectQueryManager.getDispatchProtectionDeviceCount(LocalDate.now().getYear());
        if(dispatchList.isEmpty()) {
            protectPersistManager.persistCountByTypeOverideManual(ProtectionDeviceCountPersistManager.dispatchType, false);
        }
        List<ProtectionDeviceCount> maintenanceList = protectQueryManager.getManuProtectionDeviceCount(LocalDate.now().getYear());
        if(maintenanceList.isEmpty()) {
            protectPersistManager.persistCountByTypeOverideManual(ProtectionDeviceCountPersistManager.manuType, false);
        }
        List<ProtectionDeviceCount> ovelList = protectQueryManager.getOvelProtectionDeviceCount(LocalDate.now().getYear());
        if(ovelList.isEmpty()) {
            protectPersistManager.persistCountByTypeOverideManual(ProtectionDeviceCountPersistManager.ovelType, false);
        }
    }
    /**
     * 	发电机定时统计，每天检测当年的数据是否已经统计，如果没有实时统计当年的
     */
    public void executeElectricityInfoCount() {
        List<ElectricityInfo> list = electricityGenerationManager.getElectricityInfos(LocalDate.now().getYear(), null);
        if(list.isEmpty()) {
            electricityGenerationManager.countByPrimaryDeviceCount(LocalDate.now().getYear());
        }
    }
    /**
     * 	变压器定时统计，每天检测当年的数据是否已经统计，如果没有实时统计当年的
     */
    public void executeTransmissionInfoCount() {
        List<TansformerInfo> list = transmissionEquipmentManager.getTansformerInfos(LocalDate.now().getYear(), null);
        if(list.isEmpty()) {
            transmissionEquipmentManager.countTansformerInfoByPrimaryDeviceCount(LocalDate.now().getYear());
        }
    }
    /**
     * 	母线定时统计，每天检测当年的数据是否已经统计，如果没有实时统计当年的
     */
    public void executeBusbarInfoCount() {
        List<BusbarInfo> list = transmissionEquipmentManager.getBusbarInfos(LocalDate.now().getYear(), null);
        if(list.isEmpty()) {
            transmissionEquipmentManager.countBusbarInfoByPrimaryDeviceCount(LocalDate.now().getYear());
        }
    }

    /**
     * 定时统计保护动作指标分析-》各分、子公司故障快速切除率
     */
    public void executeFaultFastRemoveCount() {
        protectKpiManager.deleteFaultFastRemove(LocalDate.now().getYear(), null, FaultFastRemove.MAINTENANCE_SCOPE, false);
        protectKpiManager.deleteFaultFastRemove(LocalDate.now().getYear(), null, FaultFastRemove.DISPATCHING_MANAGEMENT, false);
        protectKpiManager.deleteFaultFastRemove(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), FaultFastRemove.MAINTENANCE_SCOPE, false);
        protectKpiManager.deleteFaultFastRemove(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), FaultFastRemove.DISPATCHING_MANAGEMENT, false);
        List<Integer> voltages = statisticsCommonManager.getVoltages();
        for (Integer vol : voltages ) {
            protectKpiCountManager.countFaultFastRemove(LocalDate.now().getYear(), null, vol, FaultFastRemove.MAINTENANCE_SCOPE, false);
            protectKpiCountManager.countFaultFastRemove(LocalDate.now().getYear(), null, vol, FaultFastRemove.DISPATCHING_MANAGEMENT, false);
            protectKpiCountManager.countFaultFastRemove(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), vol, FaultFastRemove.MAINTENANCE_SCOPE, false);
            protectKpiCountManager.countFaultFastRemove(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), vol, FaultFastRemove.DISPATCHING_MANAGEMENT, false);
        }
    }

    /**
     * 定时统计保护动作指标分析-》各分、子公司保护正确动作率
     */
    public void executeProtectCorrectActCount() {
        protectKpiManager.deleteProtectCorrectAct(LocalDate.now().getYear(), null, ProtectCorrectAct.MAINTENANCE_SCOPE, false);
        protectKpiManager.deleteProtectCorrectAct(LocalDate.now().getYear(), null, ProtectCorrectAct.DISPATCHING_MANAGEMENT, false);
        protectKpiManager.deleteProtectCorrectAct(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), ProtectCorrectAct.MAINTENANCE_SCOPE, false);
        protectKpiManager.deleteProtectCorrectAct(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), ProtectCorrectAct.DISPATCHING_MANAGEMENT, false);
        List<Integer> voltages = statisticsCommonManager.getVoltages();
        for (Integer vol : voltages ) {
            protectKpiCountManager.countProtectCorrectAct(LocalDate.now().getYear(), null, vol, ProtectCorrectAct.MAINTENANCE_SCOPE, false);
            protectKpiCountManager.countProtectCorrectAct(LocalDate.now().getYear(), null, vol, ProtectCorrectAct.DISPATCHING_MANAGEMENT, false);
            protectKpiCountManager.countProtectCorrectAct(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), vol, FaultFastRemove.MAINTENANCE_SCOPE, false);
            protectKpiCountManager.countProtectCorrectAct(LocalDate.now().getYear(), LocalDate.now().getMonth().getValue(), vol, FaultFastRemove.DISPATCHING_MANAGEMENT, false);
        }
    }
}
